package lx.exalarm.alarm;

import android.content.Context;
import android.media.RingtoneManager;

import java.io.Serializable;

import lx.exalarm.R;
import lx.exalarm.app.ThisApp;
import lx.exalarm.database.DBManager;
import lx.exalarm.utils.StringUtils;
import lx.exalarm.utils.log.Log;

/**
 * Created by liuxu on 15-2-28.
 */
public class ActionBean implements Serializable {

    public static final int TYPE_AUDIO = 1001;
    public static final int TYPE_VIDEO = 1002;
    public static final int TYPE_IMAGE = 1003;

    public static final String DEFAULT_DATA_AUDIO =
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString();
    public static final String DEFAULT_DATA_VIDEO = "None";

    private int id;  // id from database primary key
    private int type;  // play media type
    private int interval;  // in minute, action will launch after interval
    private int durance;  // in minute, action durance. not used for now
    private String data;  // uri for media path
    private boolean enabled;  // if the action is enabled
    private int nextId;  // next action in sequence

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return this.type;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public int getInterval() {
        return this.interval;
    }

    public void setDurance(int durance) {
        this.durance = durance;
    }

    public int getDurance() {
        return this.durance;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getData() {
        return this.data;
    }

    public void setNextId(int id) {
        this.nextId = id;
    }

    public int getNextId() {
        return this.nextId;
    }

    /**
     * get action type as a readable string
     */
    public String getTypeString() {
        return getTypeString(this.getType());
    }

    /**
     * get next enabled action in the sequence, can be null
     */
    public ActionBean getNextEnabledAction() {
        ActionBean next = DBManager.getInstance().getAction(this.getNextId());
        while (next != null && !next.isEnabled() && next.getNextId() != -1) {
            next = DBManager.getInstance().getAction(next.getNextId());
        }

        Log.d("ActionBean.getNextEnabledAction, next=" + next);
        if (next != null && next.isEnabled()) {
            return next;
        } else {
            return null;
        }
    }

    /**
     * get start time for this action, in millisecond, since January 1, 1970 00:00:00.0 UTC.
     * @return millisecond
     */
    public long getStartTime() {
        return System.currentTimeMillis() + this.interval * 60 * 1000;
    }

    @Override
    public String toString() {
        return new StringBuilder().append(this.getClass().getSimpleName())
                .append("[id=").append(getId())
                .append(",type=").append(getType())
                .append(",enabled=").append(isEnabled())
                .append(",interval=").append(getInterval())
                .append(",durance=").append(getDurance())
                .append(",nextId=").append(getNextId())
                .append(",data=").append(getData())
                .append("]").toString();
    }

    /**
     * get action type as a readable string
     */
    public static String getTypeString(int type) {
        Context cxt = ThisApp.getInstance();
        switch (type) {
            case ActionBean.TYPE_AUDIO:
                return cxt.getString(R.string.action_type_audio);
            case ActionBean.TYPE_VIDEO:
                return cxt.getString(R.string.action_type_video);
            case ActionBean.TYPE_IMAGE:
                return cxt.getString(R.string.action_type_image);
            default:
                return "";
        }
    }

    public static ActionBean createDefaultAudioAction() {
        ActionBean action = new ActionBean();
        action.setId(-1);
        action.setType(TYPE_AUDIO);
        action.setInterval(0);
        action.setDurance(5);
        action.setEnabled(true);
        action.setData(DEFAULT_DATA_AUDIO);
        return action;
    }

    public static ActionBean createDefaultVideoAction() {
        ActionBean action = new ActionBean();
        action.setId(-1);
        action.setType(TYPE_VIDEO);
        action.setInterval(0);
        action.setDurance(5);
        action.setEnabled(true);
        action.setData(DEFAULT_DATA_VIDEO);
        return action;
    }

}
